// 「レシピ054: UITableViewをプルダウンしたことを感知する」のサンプルコード (P.126)

#import "HeaderView.h"


// This is defined in Math.h
#define M_PI   3.14159265358979323846264338327950288   /* pi */
// Our conversion definition
#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)


@implementation HeaderView

@synthesize label_state;
@synthesize label_datetime;

//標記:Drag Refresh HeaderView Class.m

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //0.動畫狀態設定 
        run_roatation=NO;        
        
        
        //1.文字敘述
        label_state = [[UILabel alloc]
        initWithFrame:CGRectMake(80.0f,frame.size.height-50.0f,160.0f,40.0f)];
        label_state.font = [UIFont systemFontOfSize:17.0f];
        label_state.textColor = [UIColor darkGrayColor];
        label_state.backgroundColor = [UIColor clearColor];
        label_state.textAlignment = UITextAlignmentCenter;
        label_state.text = @"Pull Down Reload";
        [self addSubview:label_state];
        
        label_datetime= [[UILabel alloc]
                       initWithFrame:CGRectMake(60.0f,frame.size.height-30.0f,200.0f,40.0f)];
        label_datetime.font = [UIFont systemFontOfSize:13.0f];
        label_datetime.textColor = [UIColor darkGrayColor];
        label_datetime.backgroundColor = [UIColor clearColor];
        label_datetime.textAlignment = UITextAlignmentCenter;
        label_datetime.text = @"Pull Down Reload";
        [self addSubview:label_datetime];       
       
        //2.箭頭向上
        
        //因為原本的箭頭符號是往上，但是要把它轉為向上 所以採用 imageRotatedByDegrees
        imageToMove =[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"blueArrow.png"] imageRotatedByDegrees:180] ];
        
        imageToMove.frame = CGRectMake(40.0f,frame.size.height-60.0f,23.0f,60.0f);
        [self addSubview:imageToMove];
        
          
         //3.進度指示器
        ActivityIndicator=[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30.0f,frame.size.height-60.0f,60.0f,60.0f)];
        
        ActivityIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
        ActivityIndicator.hidden=YES;
        
        [self  addSubview:ActivityIndicator];			
        
        [ActivityIndicator startAnimating];		        
        
        
        
    }
    return self;
}
//將imageToMove 回到原始狀態
- (void)default_image{
    
    imageToMove.transform=CGAffineTransformIdentity; 
}
//開始動畫
- (void)start_rotation{
    //如果沒有正在執行動畫時才可以執行
    if (run_roatation==NO) {
        run_roatation=YES;
        
        [self rotateImage:imageToMove duration:0.5
                    curve:UIViewAnimationCurveLinear degrees:-180];        
    }
     
} 
//開始動畫 函數
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration curve:(int)curve degrees:(CGFloat)degrees{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDelegate:self];
    // The transform matrix
    CGAffineTransform transform = 
    CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
    image.transform = transform;
    
    // Commit the changes
    [UIView commitAnimations];
}
//動畫結束時
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    //動畫結束
    run_roatation=NO;
}
//開始更新中
- (void)start_updateing{
    imageToMove.hidden=YES;
    ActivityIndicator.hidden=NO;    
}
//停止更新時 顯示 imageToMove 關閉ActivityIndicator
- (void)stop_updateing{
    imageToMove.hidden=NO;
    ActivityIndicator.hidden=YES;    
}

- (void)dealloc {
     
    [imageToMove release];
    [ActivityIndicator release];
    
    [super dealloc];
}

@end